<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <!--<meta http-equiv="refresh" content="60">-->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/js/layui/css/layui.css"  media="all">
</head>
<body>
 	<form  class="layui-form" id="form1">
 	<div class="layui-form-item" style="margin-top: 10px;margin-left: 10px;">
		电话：
		<div class="layui-inline">
	      <input type="text" name="phone" id="phone" lay-verify="required" placeholder="根据手机号码查找" autocomplete="off" class="layui-input">
		</div>
		状态：
		<div class="layui-inline">
		  <select name="status" id="status" lay-verify="status" lay-filter="status">
		 	<option selected="selected" value="">请选择状态</option>
	      	<option value="-1">已作废</option>
	      	<option value="0">刚下单</option>
	      	<option value="1">已接单</option>
	      	<option value="2">已到达</option>
	      	<option value="3">已完成</option>
		  </select>
		</div>
		<button id="find"  class="layui-btn" type="button" lay-filter="find"><i class="layui-icon">&#xe615;</i></button>
	</div>
 </form>
<table class="layui-hide" id="test" lay-filter="test"></table>


	<script type="text/html" id="toolbarDemo">
		<div class="layui-btn-container">
			<button class="layui-btn layui-btn-sm" lay-event="add">新增订单</button>
		</div>
	</script>
	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="upd">编辑</a>
		<a class="layui-btn layui-btn-xs" lay-event="sendMaster">派单</a>
		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="relieve">解除派单</a>
	</script>
	<script src="/js/layui/layui.js" charset="utf-8"></script>
<script>
	var orders = "";
layui.use(['table','layer','form','jquery'], function(){
  var table = layui.table
  ,layer = layui.layer
  ,$ = layui.jquery
  ,form=layui.form;
  table.render({
    elem: '#test'
    ,url:'/orders/find'
    ,method:'post'
    ,toolbar: '#toolbarDemo'
    ,title: '用户数据表'
    ,cols: [[
      {field:'id', title:'ID', sort: true}
      ,{field:'name', title:'客户', sort: true}
      ,{field:'phone', title:'手机号'}
      ,{field:'createtime', title:'下单时间',templet: "<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd HH:mm')}}</div>"}
      ,{field:'updtetime', title:'接单时间',templet:"<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd HH:mm:ss')}}</div>",hide : 'true'}
      ,{field:'address', title:'地址'}
      ,{field:'status', title:'状态', templet:status}
      ,{field:'endtime', title:'结单时间',templet: "<div>{{layui.util.toDateString(d.createtime,'yyyy-MM-dd HH:mm:ss')}}</div>",hide : 'true'}
      ,{ title: '操作',width:200, toolbar: '#barDemo', align: 'center'}
	  ,{field : 'lng',title : 'lng',hide : 'true'}
	  ,{field : 'lat',title : 'lat',hide : 'true'}
	  ,{field : 'car',title : 'car',hide : 'true'}
    ]]
    ,page: true
    ,id : 'orders'
	 ,parseData: function (rs) {//数据格式解析
		  if (rs.code == 200) {
			  return {
				  "code": rs.code,
				  "msg": rs.msg,
				  "count": rs.data.total,
				  "data": rs.data.list
			  }
		  }
	  },
	  response: {	// 设置响应码
		  statusCode: 200 //规定成功的状态码，默认：0
	  }
  });
  
  //搜索
  $("#find").click(function(){
       table.reload('orders', {
      	 where:{
      		status:$("#status").val(),
      		phone:$("#phone").val()
      	 }
       });
  });


//监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;//  sendMaster relieve
    if(obj.event === 'relieve'){
      layer.confirm('是否解除派单', function(index){
    	$.post("/orders/sendMaster",{"id":data.id},function(data){
    		layer.alert("解除成功！");
    	});
        window.parent.location.reload();
      });
    }else if(obj.event === 'sendMaster'){
    	orders = data;
		layer.open({
			type: 2,
			skin: 'layui-layer-demo', //样式类名
			title: "派单",
			closeBtn: 1, //不显示关闭按钮
			anim: 2,
			maxmin: true,
			area: ['100%', '100%'],
			shadeClose: true, //开启遮罩关闭
			content: '/view/orders/list.html',
			end: function () {
				table.reload('orders');
			}
		});
    }else if(obj.event === 'upd'){
		orders = data;
		open("修改订单",'/view/orders/edit.html');
	}
  });
  function open(title,url){
	  layer.open({
		  type: 2,
		  skin: 'layui-layer-demo', //样式类名
		  title: title,
		  closeBtn: 1, //不显示关闭按钮
		  anim: 2,
		  maxmin: true,
		  area: ['100%', '100%'],
		  shadeClose: true, //开启遮罩关闭
		  content: url,
		  end:function () {
			  table.reload('orders');
		  }
	  });
  }
  //工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
       case 'add':
		   var data = checkStatus.data;
		   open("添加订单",'/view/orders/add.html');
		   break;
    };
  });
});
function status(d){
	var str="";
	if(d.status=='-1'){
		str="已作废";
	}
	if(d.status=='0'){
		str="刚下单";
	}
	if(d.status=='1'){
		str="已接单";
	}
	if(d.status=='2'){
		str="已到达正在修";
	}
	if(d.status=='3'){
		str="已完成";
	}
	if(d.status=='4'){
		str="已评价";
	}
	return str;
}
</script>

</body>
</html>